package com.siwei.mes.service.erp.impl;

import com.siwei.mes.common.util.JsonUtils;
import com.siwei.mes.entity.dto.PhbhistoryPhbDto;
import com.siwei.mes.entity.dto.PhbhistoryPhbInfo;
import com.siwei.mes.entity.erp.Phbhistory;
import com.siwei.mes.entity.erp.PhbhistoryExtend;
import com.siwei.mes.entity.tgl.Tjlbycl;
import com.siwei.mes.mapper.erp.PhbhistoryExtendMapper;
import com.siwei.mes.mapper.erp.PhbhistoryMapper;
import com.siwei.mes.mapper.tgl.TjlbyclMapper;
import com.siwei.mes.service.erp.PhbhistoryExtendService;
import com.siwei.mes.sync.erp.entity.ItemorderFinish;
import org.springframework.stereotype.Service;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;

import javax.annotation.Resource;
import java.util.List;

/**
 * ERP配合比修改记录扩展信息(品控)(PhbhistoryExtend)表服务实现类
 *
 * @author makejava
 * @since 2024-12-01 15:08:27
 */
@Service
public class PhbhistoryExtendServiceImpl implements PhbhistoryExtendService {
    @Resource
    private PhbhistoryExtendMapper phbhistoryExtendMapper;
    @Resource
    private TjlbyclMapper tjlbyclMapper;
    @Resource
    private PhbhistoryMapper phbhistoryMapper;

    @Override
    public Long create(PhbhistoryExtend entity) {
        phbhistoryExtendMapper.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void update(PhbhistoryExtend entity) {
        PhbhistoryExtend phbhistoryExtend = phbhistoryExtendMapper.selectByPrimaryKey(entity.getId());
        assertTrue(phbhistoryExtend == null, "ERP配合比修改记录扩展信息(品控)信息不存在");
        phbhistoryExtendMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        PhbhistoryExtend phbhistoryExtend = phbhistoryExtendMapper.selectByPrimaryKey(id);
        assertTrue(phbhistoryExtend == null, "ERP配合比修改记录扩展信息(品控)信息不存在");
        phbhistoryExtendMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<PhbhistoryExtend> getPage(PageQuery<PhbhistoryExtend, PhbhistoryExtend> query) {
        PhbhistoryExtend searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> phbhistoryExtendMapper.getList(searchParam));
    }

    @Override
    public PhbhistoryExtend loadById(Long id) {
        return phbhistoryExtendMapper.loadById(id);
    }

    @Override
    public List<PhbhistoryExtend> getAll(PhbhistoryExtend entity) {
        return phbhistoryExtendMapper.getList(entity);
    }

    @Override
    public void savePhbhistoryExtend(ItemorderFinish itemorderFinish) {
        List<Phbhistory> phbhistoryList = phbhistoryMapper.selectByRwdh(itemorderFinish.getFRwdh(),0);
        for(Phbhistory phbhistory :phbhistoryList){
            PhbhistoryExtend phbhistoryExtend = phbhistoryExtendMapper.selectByPhid(phbhistory.getPhid());
            if(phbhistoryExtend == null){
                phbhistoryExtend.setPhid(phbhistory.getPhid());
                phbhistoryExtend.setFrwdh(itemorderFinish.getFRwdh());
                PhbhistoryPhbDto phbhistoryPhbDto = new PhbhistoryPhbDto();
                Integer jlbIds = Integer.valueOf(itemorderFinish.getJlbIds().replace("[", "").replace("]", ""));
                List<Tjlbycl> tjlbyclList = tjlbyclMapper.getList(new Tjlbycl().setFNo(jlbIds).setFPanNo(1));
                PhbhistoryExtend entity = new PhbhistoryExtend();
                entity.setFrwdh(itemorderFinish.getFRwdh());
                for (PhbhistoryPhbInfo phbInfo : phbhistory.getAfterPhbList()) {
                    for (Tjlbycl tjlbycl : tjlbyclList) {
                        /**遍历tgl盘次信息 获取第一次开票中的含水量**/
                        if (tjlbycl.getFYlmc().equals(phbInfo.getName()) && tjlbycl.getFPzgg().equals(phbInfo.getPzgg())) {
                            phbhistoryPhbDto.setHsl(tjlbycl.getFHsl() ==0 ? null:String.valueOf(tjlbycl.getFHsl()));
                        }
                    }
                }
                phbhistoryExtend.setPhbhistoryJson(JsonUtils.beanToObject(phbhistoryPhbDto));
                phbhistoryExtendMapper.insertSelective(phbhistoryExtend);
            }else{
                /**判断配合比修改记录扩展表中是否存在含水率，如果没有补充修改**/

            }
        }
    }
}
